package lnkwe.util;

import java.io.*;

/**
 * Created by lijing on 2016-03-17.
 */
public class FileIOUtil {

    public static final int defaultBufSize = 1024 ;

    public static int write(InputStream in, OutputStream out)throws IOException {
        int fileSize=write(in, out, 0);
        return fileSize;
    }

    public static int write(InputStream in, OutputStream out, int bufSize)throws IOException {
        int fileSize=0;
        int _bufSize = (bufSize != 0) ? bufSize : defaultBufSize;
        byte[] buffer = new byte[_bufSize];
        int len;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        fileSize=in.available();
        out.flush();
        out.close();
        in.close();
        return fileSize;
    }

    public static String getText(InputStream stream) {
        String result = "";

        StringBuffer sb = new StringBuffer();
        try {

            BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = in.readLine()) != null) {
                buffer.append(line);
            }
            result = buffer.toString();

        } catch (IOException ioe) {
            ioe.printStackTrace();
            ;
        }

        return result;
    }



}
